package cn.tp.ksoft.civil_driver.view.widget.tablayou;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;

import cn.tp.ksoft.civil_driver.R;

/**
 * Created by zhusong on 16/4/26.
 * Tab Item content view, expand item info in this view
 */
public class ITabItem extends View {

    private static final float DEFAULT_TEXT_SIZE = 12f;    //sp
    private static final float DEFAULT_ICON_SIZE = 14f;//dp
    private static final float DEFAULT_ITEM_H_PADDING = 6f;//dp
    private static DisplayMetrics dm;
    protected CharSequence mText;
    protected Drawable mIcon;
    protected int mIconSize;
    protected int mOrientation;
    protected int mTextSize;
    protected ColorStateList mTextColors;

    protected int mLeftPadding;
    protected int mTopPadding;
    protected int mRightPadding;
    protected int mBottomPadding;

    protected int mIconLeftMargin;
    protected int mIconTopMargin;
    protected int mIconRightMargin;
    protected int mIconBottomMargin;

    protected int mTextAppearance;
    protected static int mDefaultStyleRes = R.style.tabItemDefaultStyle;
    public ITabItem(Context context) {
        this(context, null);
    }

    public ITabItem(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ITabItem(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        if (null == dm) {
            dm = getResources().getDisplayMetrics();
        }

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ITabItem,
                defStyleAttr, mDefaultStyleRes);
        // text attributes
        mText = ta.getText(R.styleable.ITabItem_android_text);
        final int textSize = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                DEFAULT_TEXT_SIZE, dm));
        mTextSize = ta.getDimensionPixelSize(R.styleable.ITabItem_android_textSize, textSize);
        ColorStateList textColors = ta.getColorStateList(R.styleable.ITabItem_android_textColor);
        if (null != textColors) {
            mTextColors = textColors;
        }
        mIcon = ta.getDrawable(R.styleable.ITabItem_android_icon);

        // icon attributes
        mOrientation = ta.getInt(R.styleable.ITabItem_android_orientation,
                LinearLayout.HORIZONTAL);
        mIconLeftMargin = mIconTopMargin = mIconRightMargin = mIconBottomMargin
                = ta.getDimensionPixelSize(R.styleable.ITabItem_tabIconMargin, 0);
        mIconLeftMargin = ta.getDimensionPixelSize(R.styleable.ITabItem_tabIconMarginLeft, 0);
        mIconTopMargin = ta.getDimensionPixelSize(R.styleable.ITabItem_tabIconMarginTop, 0);
        mIconRightMargin = ta.getDimensionPixelSize(R.styleable.ITabItem_tabIconMarginRight, 0);
        mIconBottomMargin = ta.getDimensionPixelSize(R.styleable.ITabItem_tabIconMarginBottom, 0);

        final int padding = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                DEFAULT_ITEM_H_PADDING, dm));
        mLeftPadding = mTopPadding = mRightPadding = mBottomPadding
                = ta.getDimensionPixelSize(R.styleable.ITabItem_iTabPadding, padding);
        mLeftPadding = ta.getDimensionPixelSize(R.styleable.ITabItem_iTabPaddingLeft, padding);
        mTopPadding = ta.getDimensionPixelSize(R.styleable.ITabItem_iTabPaddingTop, padding);
        mRightPadding = ta.getDimensionPixelSize(R.styleable.ITabItem_iTabPaddingRight, padding);
        mBottomPadding = ta.getDimensionPixelSize(R.styleable.ITabItem_iTabPaddingBottom, padding);

        final int iconSize = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                DEFAULT_ICON_SIZE, dm));
        mIconSize = ta.getDimensionPixelSize(R.styleable.ITabItem_iconSize, iconSize);

        mTextAppearance = ta.getResourceId(R.styleable.ITabItem_android_textAppearance, android.R.style.TextAppearance_DeviceDefault);
        ta.recycle();
    }
}
